/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.personassessmen.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.assessment.entity.AssessmentTime;
import com.fosung.frame.modules.assessment.service.AssessmentTimeService;
import com.fosung.frame.modules.partytype.entity.PartyType;
import com.fosung.frame.modules.partytype.service.PartyTypeService;
import com.fosung.frame.modules.personassessmen.entity.PersonAssessment;
import com.fosung.frame.modules.personassessmen.service.PersonAssessmentService;
import com.fosung.frame.modules.personassessmentinfo.entity.PersonAssessmentInfo;
import com.fosung.frame.modules.personassessmentinfo.service.PersonAssessmentInfoService;

/**
 * 党员考核Controller
 * 
 * @author 吴
 * @version 2017-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/personassessmen/personAssessment")
public class PersonAssessmentController extends BaseController {

	@Autowired
	private PersonAssessmentService personAssessmentService;
	@Autowired
	private AssessmentTimeService assessmentTimeService;
	@Autowired
	private PartyTypeService partyTypeService;
	@Autowired
	private PersonAssessmentInfoService personAssessmentInfoService;

	@ModelAttribute
	public PersonAssessment get(@RequestParam(required = false) String id) {
		PersonAssessment entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = personAssessmentService.get(id);
		}
		if (entity == null) {
			entity = new PersonAssessment();
		}
		return entity;
	}

	@RequiresPermissions("personassessmen:personAssessment:view")
	@RequestMapping(value = { "list", "" })
	public String list(PersonAssessment personAssessment, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<PersonAssessment> page = personAssessmentService.findPage(new Page<PersonAssessment>(request, response),
				personAssessment);
		model.addAttribute("page", page);
		return "modules/personassessmen/personAssessmentList";
	}

	@RequiresPermissions("personassessmen:personAssessment:view")
	@RequestMapping(value = "form")
	public String form(PersonAssessment personAssessment, Model model) {
		model.addAttribute("personAssessment", personAssessment);
		AssessmentTime at=new AssessmentTime();
		at.setState("1");
		at.setDelFlag("0");
		List<AssessmentTime> assessmentlist=assessmentTimeService.findList(at);
		PartyType pt=new PartyType();
		pt.setState("1");
		pt.setDelFlag("0");
		List<PartyType> partyTypelist = partyTypeService.findList(pt);
		model.addAttribute("assessmentlist", assessmentlist);
		model.addAttribute("partyTypelist", partyTypelist);
		return "modules/personassessmen/personAssessmentForm";
	}

	@RequiresPermissions("personassessmen:personAssessment:edit")
	@RequestMapping(value = "save")
	public String save(PersonAssessment personAssessment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, personAssessment)) {
			return form(personAssessment, model);
		}
		personAssessmentService.save(personAssessment);
		addMessage(redirectAttributes, "保存党员考核成功");
		return "redirect:" + Global.getAdminPath() + "/personassessmen/personAssessment/?repage";
	}

	@RequiresPermissions("personassessmen:personAssessment:edit")
	@RequestMapping(value = "delete")
	public String delete(PersonAssessment personAssessment, RedirectAttributes redirectAttributes) {
		personAssessmentService.delete(personAssessment);
		addMessage(redirectAttributes, "删除党员考核成功");
		return "redirect:" + Global.getAdminPath() + "/personassessmen/personAssessment/?repage";
	}

	@RequiresPermissions("personassessmen:personAssessment:edit")
	@RequestMapping(value = "updateStart")
	public String updateStart(PersonAssessment personAssessment, RedirectAttributes redirectAttributes) {
		personAssessmentService.updateStart(personAssessment);
		addMessage(redirectAttributes, "启动党员考核成功");
		return "redirect:" + Global.getAdminPath() + "/personassessmen/personAssessment/?repage";
	}

	@RequiresPermissions("personassessmen:personAssessment:edit")
	@RequestMapping(value = "updateStop")
	public String updateStop(PersonAssessment personAssessment, RedirectAttributes redirectAttributes) {
		personAssessmentService.updateStop(personAssessment);
		addMessage(redirectAttributes, "关闭党员考核成功");
		return "redirect:" + Global.getAdminPath() + "/personassessmen/personAssessment/?repage";
	}

	@RequiresPermissions("personassessmen:personAssessment:view")
	@RequestMapping(value = { "listPersonAssessmentInfo" })
	public String listPersonAssessmentInfo(PersonAssessmentInfo personAssessmentInfo, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		PersonAssessment personAssessment = personAssessmentService.get(personAssessmentInfo.getPersonAssessment());
		model.addAttribute("personAssessment", personAssessment);
		Page<PersonAssessmentInfo> page = personAssessmentInfoService
				.findPage(new Page<PersonAssessmentInfo>(request, response), personAssessmentInfo);
		model.addAttribute("page", page);
		return "modules/personassessmentinfo/personAssessmentInfoList";
	}

	@RequiresPermissions("personassessmen:personAssessment:view")
	@RequestMapping(value = "formPersonAssessmentInfo")
	public String formPersonAssessmentInfo(PersonAssessmentInfo personAssessmentInfo, Model model) {
		if(personAssessmentInfo.getId()!=null&&!"".equals(personAssessmentInfo.getId())){
			personAssessmentInfo=personAssessmentInfoService.get(personAssessmentInfo.getId());
		}
		model.addAttribute("personAssessmentInfo", personAssessmentInfo);
		return "modules/personassessmentinfo/personAssessmentInfoForm";
	}

	@RequiresPermissions("personassessmen:personAssessment:edit")
	@RequestMapping(value = "savePersonAssessmentInfo")
	public String savePersonAssessmentInfo(PersonAssessmentInfo personAssessmentInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, personAssessmentInfo)) {
			return formPersonAssessmentInfo(personAssessmentInfo, model);
		}
		personAssessmentInfoService.save(personAssessmentInfo);
		addMessage(redirectAttributes, "保存党员考核积分设置成功");
		return "redirect:" + Global.getAdminPath() + "/personassessmen/personAssessment/listPersonAssessmentInfo/?personAssessment="+
		personAssessmentInfo.getPersonAssessment();
	}
	@RequiresPermissions("personassessmen:personAssessment:edit")
	@RequestMapping(value = "deletePersonAssessmentInfo")
	public String deletePersonAssessmentInfo(PersonAssessmentInfo personAssessmentInfo, RedirectAttributes redirectAttributes) {
		personAssessmentInfo=personAssessmentInfoService.get(personAssessmentInfo);
		String personAssessment=personAssessmentInfo.getPersonAssessment();
		personAssessmentInfoService.delete(personAssessmentInfo);
		addMessage(redirectAttributes, "删除党员考核积分设置成功");
		return "redirect:" + Global.getAdminPath() + "/personassessmen/personAssessment/listPersonAssessmentInfo/?personAssessment="+personAssessment;
	}
}